package com.eco.fanliapp.shengyaappeurekeclient.utils;

import com.alibaba.fastjson.JSON;
import com.eco.fanliapp.shengyaappeurekeclient.entity.AppGoods;
import com.eco.fanliapp.shengyaappeurekeclient.service.IAppGoodsService;
import org.apache.lucene.document.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class GetDocument {

    @Autowired
    private IAppGoodsService appGoodsService;

    public Document getDocument(AppGoods appGoods) {
        List list = new ArrayList<>();
        list.add(appGoods);
        BigDecimal price = (BigDecimal) appGoodsService.getPrice(null, list);
        //创建Document对象
        Document doc = new Document();
        //获取每列数据
        Field itemid = new Field("itemid", appGoods.getItemid(), TextField.TYPE_STORED);
        Field itemtitle = new Field("itemtitle", appGoods.getItemtitle() == null ? "" : appGoods.getItemtitle(), StringField.TYPE_STORED);
        Field itemshorttitle = new Field("itemshorttitle", appGoods.getItemshorttitle() == null ? "" : appGoods.getItemshorttitle(), StringField.TYPE_STORED);
        Field itemdesc = new Field("itemdesc", appGoods.getItemdesc() == null ? "" : appGoods.getItemdesc(), StringField.TYPE_STORED);
        Field itemObject = new Field("itemObject", JSON.toJSONString(appGoods), TextField.TYPE_STORED);

        Field fqcat = new Field("fqcat", appGoods.getFqcat() == null ? "" : appGoods.getFqcat(), StringField.TYPE_STORED);
        Field recommend = new Field("recommend", appGoods.getRecommend() == null ? "" : appGoods.getRecommend().toString(), StringField.TYPE_STORED);
        Field itemTop = new Field("itemTop", appGoods.getItemTop() == null ? "" : appGoods.getItemTop().toString(), StringField.TYPE_STORED);


        //销量
        Field itemsales = new FloatPoint("total_sales", (float) (appGoods.getItemsale() == null ? 0 : appGoods.getItemsale()));
        //价格  到手价
        Field itemprice = new FloatPoint("price", (price == null ? 0 : price).floatValue());
        // 综合 更新时间
        Field updateTime = new FloatPoint("tk_total_commi", (appGoods.getUpdateTime() == null ? 0 : appGoods.getUpdateTime().getTime()));
        //最省钱 优惠卷加联盟返佣
        BigDecimal commiCoupon = new BigDecimal(appGoods.getTkmoney() == null ? "0" : appGoods.getTkmoney()).add(new BigDecimal(appGoods.getCouponmoney()));
        Field itemCheaper = new FloatPoint("tk_rate", Float.valueOf(commiCoupon.toString() == null ? "0" : commiCoupon.toString()));
        if (price == null || price.intValue() == 0) {
            return null;
        }
        //销量
        doc.add(new FloatDocValuesField("total_sales", (float) (appGoods.getItemsale() == null ? 0 : appGoods.getItemsale()) * 1000F));
        doc.add(new StoredField("total_sales", (float) (appGoods.getItemsale() == null ? 0 : appGoods.getItemsale())));
        //价格
        doc.add(new StoredField("price", (price == null ? 0 : price).floatValue()));//存储
        doc.add(new FloatDocValuesField("price", ((price == null ? 0 : price).floatValue() * 1000F)));//排序
        //优惠卷加联盟返佣
        doc.add(new FloatDocValuesField("tk_rate", Float.valueOf(commiCoupon.toString() == null ? "0" : commiCoupon.toString()) * 1000F));
        doc.add(new StoredField("tk_rate", Float.valueOf(commiCoupon.toString() == null ? "0" : commiCoupon.toString())));
        //更新时间
        doc.add(new FloatDocValuesField("tk_total_commi", (appGoods.getUpdateTime() == null ? 0 : appGoods.getUpdateTime().getTime())));
        doc.add(new StoredField("tk_total_commi", (appGoods.getUpdateTime() == null ? 0 : appGoods.getUpdateTime().getTime())));


        doc.add(itemid);
        doc.add(itemtitle);
        doc.add(itemshorttitle);
        doc.add(itemdesc);
        doc.add(itemObject);
        doc.add(itemsales);
        doc.add(itemprice);
        doc.add(itemCheaper);
        doc.add(updateTime);
        doc.add(fqcat);
        doc.add(recommend);
        doc.add(itemTop);
        return doc;
    }

}
